CROSS_COMPILE_PREFIX = 
MODE := DEBUG

SRC_DIR := .
TMP_DIR := tmp

USER_BIN_NAME := out.elf
SRCS := .
INC_PATH := .

USER_SRC_PATH = $(addprefix $(SRC_DIR) ,$(SRCS))
USER_INC_PATH = $(addprefix $(SRC_DIR) ,$(INC_PATH))

SRC_FILES := $(foreach dir, $(USER_SRC_PATH), $(wildcard $(dir)/*.c))
TMP_FILES := $(patsubst $(SRC_DIR)/%,$(TMP_DIR)/%, $(SRC_FILES))
OBJ_FILES := $(TMP_FILES:.c=.o)
DEP_FILES := $(OBJ_FILES:.o=.d)

#FLAGS := -Wall -Werror
FLAGS :=
FLAGS += $(addprefix -I ,$(USER_INC_PATH))
FLAGS += -std=gnu99

ifeq ($(MODE), DEBUG)
FLAGS += -g -MMD
else
FLAGS += -O2 -DNDEBUG
endif

LDFLAGS += -Wl,--copy-dt-needed-entries -lpthread -lm


$(warning FLAGS: $(FLAGS))

.PHONY : all clean

all : $(USER_BIN_NAME)

$(TMP_DIR)/%.o: $(SRC_DIR)/%.c
	$(shell if [ ! -d $(dir $@) ];then mkdir -p $(dir $@); fi)
	@$(CROSS_COMPILE_PREFIX)gcc -c -o $@ $(FLAGS) $< 
	@echo -e '\033[32m"compiling gcc" : $<\033[0m'


-include $(DEP_FILES)
$(USER_BIN_NAME) : $(OBJ_FILES)
	$(shell if [ ! -d $(dir $@) ];then mkdir -p $(dir $@); fi)
	$(CROSS_COMPILE_PREFIX)gcc -o $@ $^ $(LDFLAGS)

	
clean :
	@echo -e '\033[32m"clean"\033[0m'
	@rm -rf $(shell find -name "*.o")
	@rm -rf $(shell find -name "*.d")
	@rm -rf $(USER_BIN_NAME)

	

